package com.qy127.dao.impl;

import com.qy127.dao.RoleDao;
import com.qy127.entity.Role;
import com.qy127.util.JDBCUtil;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Repository
public class RoleDaoImpl implements RoleDao {
    @Override
    public List<Map<String, Object>> getAllRole() {
        String sql ="select * from t_role";
        List<Map<String, Object>> maps = JDBCUtil.queryListMap(sql);
        if (maps.size()>0){
            return maps;
        }
        return null;
    }

    @Override
    public int getAllCount(Role role) {
        String sql="select count(*) from t_role";
        List<List> lists = JDBCUtil.queryList(sql);
        if (lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }

    @ResponseBody
    @Override
    public List<Map<String, Object>> getAllRoleType() {
        String sql="select ro.rid,ro.rname,ro.memo,ro.status from t_role ro";
        List<Map<String, Object>> list = JDBCUtil.queryListMap(sql);
        if (list.size()>0){
            return list;
        }
        return null;
    }

    @Override
    public int insertRole(Role role) {
        String sql = "insert into t_role values(null,?,?,?)";
        int i = JDBCUtil.update(sql, role.getRname(), role.getMemo(), role.getStatus());
        return i;
    }

    @Override
    public int updateRole(Role role) {
        String sql = "update t_role set rname=?,memo = ? , status =? where rid =?";
        int i = JDBCUtil.update(sql, role.getRname(), role.getMemo(), role.getStatus(), role.getRid());

        return i;
    }
}
